layui.use(['jquery', 'element', 'layer', 'form', 'layedit', 'upload'], function(){
    var $ = layui.$;
    var layedit = layui.layedit;
    var upload = layui.upload;
    var form = layui.form;
    var ctxPath = "/education";

    var uploadInst = upload.render({
        elem: '#showImage',
        url: ctxPath + '/file/uploadPic',
        before: function(obj){
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
                $('#previewImg').attr('src', result); //图片链接（base64）
            });
        }
        ,done: function(res){
            //如果上传失败
            if(res.code > 0){
                return layer.msg('上传失败');
            }
            //上传成功
            $("[name='showImage']").val(res.data.src);
        }
        ,error: function(){
            //演示失败状态，并实现重传
            var demoText = $('#previewText');
            demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini img-reload">重试</a>');
            demoText.find('.img-reload').on('click', function(){
                uploadInst.upload();
            });
        }
    });

    /**
     * 详情富文本
     */
    var detailEditId = layedit.build('detail', {
        height: 500,
        uploadImage :  {
            url: ctxPath + '/file/uploadPic',
            type: 'post',
        }
    });

    /**
     * 初始化积分商品详情方法
     * @param id
     */
    var initPointRewardProduct = function(id) {
        $.ajax({
            url: ctxPath + '/api/pointRewardProduct/detail',
            type: 'post',
            dataType: 'json',
            data: {pointRewardProductId: id},
            success: function (result) {
                if(result.code == 0) {
                    var data = result.data;

                    if (data != null) {
                        $("input[name='id']").val(data.id);
                        $("input[name='name']").val(data.name);
                        $("input[name='code']").val(data.code);
                        $("#previewImg").attr("src", data.showImage);
                        $("input[name='showImage']").val(data.showImage);
                        $("input[name='price']").val(data.price);
                        $("input[name='amount']").val(data.amount);

                        layedit.setContent(detailEditId, data.detail);

                        //渲染select
                        form.render('select')
                    }
                } else {
                    layer.alert("get data fail：" + result.msg);
                }
            }
        });
    };

    /**
     * 初始化积分商品
     * @type {string}
     */
    var PointRewardProductId = window.location.search.substring(4);
    if("" != PointRewardProductId){
        initPointRewardProduct(PointRewardProductId);
    }

    /**
     * 新增游学产品
     */
    $("#saveBtn").click(function () {

        var id = $("[name='id']").val();
        var name = $("[name='name']").val();
        var showImage = $("[name='showImage']").val();
        var code = $("[name='code']").val();
        var price = $("[name='price']").val();
        var amount = $("[name='amount']").val();
        var detail = layedit.getContent(detailEditId);
        var request = {};

        if(null != id && "" != id){
            request.id = id;
        }

        request.name = name;
        request.showImage = showImage;
        request.code = code;
        request.price = price;
        request.amount = amount;
        request.detail = detail;

        $.ajax({
            url: ctxPath + "/api/pointRewardProduct/sync",
            data: JSON.stringify(request),
            type: 'post',
            dataType: 'json',
            contentType: 'application/json',
            success: function (result) {
                if(result.code == 0) {
                    layer.alert('保存成功！', function(index){
                        layer.close(index);
                        location.href = ctxPath + '/html/pointReward/pointRewardList.html';
                    });
                } else {
                    layer.alert("保存失败：" + result.msg);
                }
            }
        });
    });


});

